package com.dragon.graduation.projectProgress.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.config.exception.exception.GlobalException;
import com.dragon.graduation.project.entity.ProjectEntity;
import com.dragon.graduation.project.service.ProjectService;
import com.dragon.graduation.projectProgress.controller.dto.ProjectProgressAddDTO;
import com.dragon.graduation.projectProgress.controller.dto.ProjectProgressSearchDTO;
import com.dragon.graduation.projectProgress.controller.dto.ProjectProgressUpdateDTO;
import com.dragon.graduation.projectProgress.controller.vo.ProjectProgressVO;
import com.dragon.graduation.projectProgress.entity.ProjectProgressEntity;
import com.dragon.graduation.projectProgress.service.ProjectProgressService;
import com.dragon.graduation.user.controller.vo.UserVO;
import com.dragon.jwt.JwtUtil;
import com.dragon.results.page.ResultPage;
import com.dragon.results.result.HttpResult;
import com.dragon.util.MyPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 毕设进度表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2025-02-28 21:20:30
 */
@RestController
@RequestMapping("/projectProgress")
@RequiredArgsConstructor
@Api(tags = "毕设进度表 相关接口")
public class ProjectProgressController {

    private final ProjectProgressService projectProgressService;
    private final ProjectService projectService;

    @PostMapping("add")
    @ApiOperation("1]毕设进度表添加")
    @ApiOperationSupport(order = 1)
    public HttpResult<String> add(@RequestBody ProjectProgressAddDTO addDTO) {
        ProjectProgressEntity entity = ProjectProgressAddDTO.to(addDTO, ProjectProgressEntity.class);
        projectProgressService.save(entity);
        return HttpResult.ok("添加成功");
    }

    @PostMapping("update")
    @ApiOperation("2]毕设进度表修改")
    @ApiOperationSupport(order = 2)
    public HttpResult<String> update(@RequestBody ProjectProgressUpdateDTO updateDTO) {
        ProjectProgressEntity entity = projectProgressService.getById(updateDTO.getId());
        Assert.isFalse(ObjectUtils.isEmpty(entity), () -> new GlobalException("要修改的数据不存在"));
        entity = ProjectProgressUpdateDTO.to(updateDTO, ProjectProgressEntity.class);
        projectProgressService.updateById(entity);
        return HttpResult.ok("修改成功");
    }

    @PostMapping("search")
    @ApiOperation("3]毕设进度表分页查询")
    @ApiOperationSupport(order = 3)
    public HttpResult<ResultPage<List<ProjectProgressVO>>> search(@RequestBody ProjectProgressSearchDTO searchDTO) {
        UserVO userVO = JwtUtil.parseToken(JwtUtil.getTokenFromRequest(), UserVO.class);
        if (userVO.getRole() != 1) {
            if (userVO.getRole() == 2) {
                searchDTO.setStudentId(userVO.getRoleId());
            } else {
                //老师看自己项目下的所有
                List<ProjectEntity> projectEntities = projectService.findByTeacherId(userVO.getRoleId());
                if (!CollectionUtils.isEmpty(projectEntities)) {
                    List<Integer> collected = projectEntities.stream().map(ProjectEntity::getId).collect(Collectors.toList());
                    searchDTO.setProjectList(collected);
                }
            }
        }

        Page<ProjectProgressEntity> entityPage = MyPage.setPage(ProjectProgressEntity.class, searchDTO.getPage());
        Page<ProjectProgressVO> page = projectProgressService.findPages(entityPage, searchDTO);
        ResultPage<List<ProjectProgressVO>> resultPage = MyPage.copyPage(page, ProjectProgressVO.class);
        return HttpResult.okForData(resultPage);
    }

    @PostMapping("delete")
    @ApiOperation("4]毕设进度表删除")
    @ApiOperationSupport(order = 4)
    public HttpResult<String> delete(@RequestBody List<Integer> idList) {
        projectProgressService.removeByIds(idList);
        return HttpResult.ok("删除成功");
    }
}
